package io.renren.modules.biz.tongtong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.biz.tongtong.dao.LockUserDao;
import io.renren.modules.biz.tongtong.dto.LockUserDTO;
import io.renren.modules.biz.tongtong.dto.TbUserDTO;
import io.renren.modules.biz.tongtong.entity.LockEntity;
import io.renren.modules.biz.tongtong.entity.LockUserEntity;
import io.renren.modules.biz.tongtong.service.LockUserService;
import cn.hutool.core.util.StrUtil;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.enums.SuperAdminEnum;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 锁授权用户
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-01-07
 */
@Service
public class LockUserServiceImpl extends CrudServiceImpl<LockUserDao, LockUserEntity, LockUserDTO> implements LockUserService {

    @Override
    public QueryWrapper<LockUserEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String lockId = (String)params.get("lockId");
        QueryWrapper<LockUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.eq(StrUtil.isNotBlank(lockId), "lock_id", lockId);
        return wrapper;
    }

    /**
     * 分页查询
     * @param params
     * @return
     */
    @Override
    public PageData<LockUserDTO> page(Map<String, Object> params) {

        //分页
        IPage<LockUserEntity> page = getPage(params, Constant.CREATE_DATE, false);
        //查询
        List<LockUserDTO> list = baseDao.getUserList(params);
        return getPageData(list, page.getTotal(), LockUserDTO.class);
    }

    /**
     * 用户列表
     * @param params
     * @return
     */
    @Override
    public List<TbUserDTO> userlist(Map<String, Object> params) {
        return baseDao.getAllUser(params);
    }

    /**
     * 根据用户id和锁id查询用户
     * @param userId
     * @param lockId
     * @return
     */
    @Override
    public LockUserEntity getByUserAndLock(Long userId, Long lockId) {
        QueryWrapper<LockUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("lock_id",lockId);
        return baseDao.selectOne(wrapper);
    }


}